class @AdminCategoryListingView extends ListingView

    el: $('#content')

    initialize: ->
        super
        @model.bind("change", @render)
        @model.fetch()

    render: ->
        model = @model
        el = $(@el)

        @Navigate(model.url(), false, true)

        @GetTemplate(SiteMap.Admin.Categories.ListingTemplate, (compiledTemplate) ->
            el.html(compiledTemplate(model.toJSON()))
        )
    events:
        "click .search": "search"
        "click .pagination a": "pagination"
        "click th>a": "sort"
        "click .buttons>.create": "create"
        "click .buttons>.delete": "remove"
        "click tbody tr>td:not(.chk)": "edit"
        "change .page-size": "changeSize"
        "click .reset": "reset"

    search: (event) ->
        event.preventDefault()
        Messages.WriteMessage('Apply search.')
        Loading.Start()
        filter = @model.get('Filter')
        filter.Name = $('#Filter_Name').val()
        @model.set("Filter", filter, { silent: true })
        @model.set("Page", 1, { silent: true })
        @model.fetch()

    reset: (event) ->
        Loading.Start()
        $("#Filter_Name").val('')
        @model.set("Filter", {Name: ''}, { silent: true })
        @model.set("Page", 1, { silent: true })
        @model.fetch()

    remove: (event) ->
        event.preventDefault()
        Messages.Clear()
        form = $(@el).find("form.listing")

        if form.find("input:checked").length
            categoryDeleteConfirm = new CategoryDeleteConfirm(@);
            categoryDeleteConfirm.Show("Are you sure?")
        else
            Messages.WriteMessage('Choose one or more author.')

     create: (event) ->
        event.preventDefault()
        Loading.Start()
        @Navigate(SiteMap.Admin.Categories.Create, true)

class CategoryDeleteConfirm extends ConfirmDialog
    @view:null;
    constructor: (view) ->
        @view=view
    Ok:()->
            form = $(@view.el).find("form.listing")
            data = form.serialize()
            Loading.Start()
            Messages.WriteMessage('Deleting category.')
            $.post(SiteMap.Admin.Categories.Delete, data, (response)=>
                if response.SuccessMessage? && response.SuccessMessage.length
                    Messages.WriteSuccessMessage(response.SuccessMessage)
                    @view.model.fetch()
                else 
                    Messages.WriteErrors(response.Errors)
                    Loading.Stop()
            )